package biz.gieraltowski.lukships;

import java.awt.event.KeyEvent;

public class Ship extends Actor {
	private boolean left, right;
	protected static final int SHIP_SPEED = 10;
	
	public Ship(Stage stage) {
		super(stage);
		setSpriteName("/ship.png");
	}
	
	public void updateSpeed(){
		if(left) x-=SHIP_SPEED;
		if(right) x+=SHIP_SPEED;
	}
	
	public void act() {
		if(x>Stage.WIDTH-width) x=Stage.WIDTH-width-8;
		else if(x<=3) x=3;
	}
	
	public void attack(){
		Bullet bullet = new Bullet(stage);
		bullet.setX(this.getX()+this.getWidth()/2-bullet.width/2);
		bullet.setY(Stage.HEIGHT-110);
		stage.addActor(bullet);
	}
	
	public void keyPressed(KeyEvent e){
		switch(e.getKeyCode()){
			case 37: left=true; break;
			case 39: right=true; break;
			case 32: attack(); break; //spacja
		}
	}
	
	public void keyReleased(KeyEvent e){
		switch(e.getKeyCode()){
			case 37: left=false; break;
			case 39: right=false; break;
		}
	}

}
